package cn.xyy.system.dao;

import java.util.List;

import cn.xyy.jdbc.BaseDao;
import org.springframework.stereotype.Repository;

import cn.xyy.common.domain.mvc.Page;
import cn.xyy.common.domain.system.notice.Notice;
import cn.xyy.common.domain.system.notice.NoticeVO;
import cn.xyy.common.utils.StringUtils;

/**
 * 通知数据持久层
 * 
 * @author xyy
 *
 */
@Repository
public class NoticeDao extends BaseDao {

	public Page<Notice> selectPage(NoticeVO vo) {
		List<Notice> list = super.select(
				getSqlPageHandle().handlerPagingSQL(pageSql(vo, 0), vo.getPage(), vo.getLimit()), null, Notice.class);
		int count = super.getJdbcTemplate().queryForObject(pageSql(vo, 1), null, Integer.class);
		Page<Notice> page = new Page<Notice>(list, count, vo.getLimit(), vo.getPage());
		return page;
	}

	private String pageSql(NoticeVO vo, int type) {
		StringBuilder sql = new StringBuilder();
		if (type == 0) {
			sql.append("select  * from t_sys_notice");
		} else {
			sql.append("select count(*) from t_sys_notice ");
		}
		sql.append(" where 1=1");

		if (StringUtils.isNotBlank(vo.getStarttime()) && StringUtils.isNotBlank(vo.getEndtime())) {
			sql.append(" and DATE_FORMAT(create_time,'%Y-%m-%d') >='" + vo.getStarttime() + "'");
			sql.append(" and DATE_FORMAT(create_time,'%Y-%m-%d') <='" + vo.getEndtime() + "'");
		}
		if (StringUtils.isNotBlank(vo.getTitle())) {
			sql.append(" and title like '%").append(vo.getTitle() + "%' ");
		}
		if (vo.getStatus() != null) {
			sql.append(" and status=" + vo.getStatus() + "");
		}

		if (type == 0) {
			sql.append(" order by create_time desc");
		}
		return sql.toString();
	}
}
